package com.shitou.shitoumember.controller;

import java.util.Arrays;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.shitou.shitoucommons.annotation.CheckUserLogin;
import com.shitou.shitoucommons.vo.MessageVo;
import com.shitou.shitoumember.config.MessagingRoom;
import com.shitou.shitoumember.entity.MemberCollectSpuEntity;
import com.shitou.shitoumember.entity.MemberEntity;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.shitou.shitoumember.entity.MemberExchangeEntity;
import com.shitou.shitoumember.service.MemberExchangeService;
import com.shitou.shitoucommons.utils.PageUtils;
import com.shitou.shitoucommons.utils.RespBean;

import javax.websocket.server.PathParam;


/**
 * 关注表
 *
 * @author shitouren
 * @email 3076767823@qq.com
 * @date 2023-04-04 16:23:15
 */
@RestController
@RequestMapping("shitoumember/memberexchange")
public class MemberExchangeController {
    @Autowired
    private MemberExchangeService memberExchangeService;

    @Autowired
    private MessagingRoom messagingRoom;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public RespBean list(@RequestParam Map<String, Object> params){
        PageUtils page = memberExchangeService.queryPage(params);

        return RespBean.success().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public RespBean info(@PathVariable("attrId") Long attrId){
        MemberExchangeEntity attr = memberExchangeService.getById(attrId);

        return RespBean.success().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RespBean save(@RequestBody MemberExchangeEntity attr){
        memberExchangeService.save(attr);

        return RespBean.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RespBean update(@RequestBody MemberExchangeEntity attr){
        memberExchangeService.updateById(attr);

        return RespBean.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RespBean delete(@RequestBody Long[] attrIds){
        memberExchangeService.removeByIds(Arrays.asList(attrIds));

        return RespBean.success();
    }

    /**
     * 获取当前会员和另一个会员的聊天记录
     * @param memberId
     * @param receiveMemberId
     * @return
     */
    @ApiOperation(value = "获取当前会员和另一个会员的聊天记录")
    @RequestMapping("/getMemberExchange")
    @CheckUserLogin
    public RespBean getMemberExchange(@RequestParam Long memberId, @RequestParam Long receiveMemberId){
        return memberExchangeService.getMemberExchange(memberId, receiveMemberId);
    }


    /**
     * 获得当前登录会员与之聊天得人得列表
     * @param memberId
     * @return
     */
    @ApiOperation(value = "获得当前登录会员与之聊天得人得列表")
    @RequestMapping("/getChatNumber")
    @CheckUserLogin
    public RespBean getChatNumber(@RequestParam Long memberId){
        return memberExchangeService.getChatNumber(memberId);
    }


    /**
     * 构建站内信，可远程调用，也可直接调用
     * @param messageVo
     * @return
     */
    @ApiOperation(value = "构建站内信，可远程调用，也可直接调用")
    @RequestMapping("/createMessaging/{account}")
    public RespBean createMessaging(MessageVo messageVo){
        messagingRoom.onMessage(JSON.toJSONString(messageVo));   // 发送站内信
        return memberExchangeService.createMessaging(messageVo);
    }


    /**
     * 获得登录用户的所有站内信信息
     * @param memberId
     * @return
     */
    @ApiOperation(value = "获得登录用户的所有站内信信息")
    @RequestMapping("/getMessaging")
    @CheckUserLogin
    public RespBean getMessaging(@RequestParam Long memberId){
        return memberExchangeService.getMessaging(memberId);
    }


    /**
     * 统计未读取的站内信数量
     * @param memberId
     * @return
     */
    @ApiOperation(value = "统计未读取的站内信数量")
    @CheckUserLogin
    @RequestMapping("/messagingCount")
    public RespBean messagingCount(@RequestParam Long memberId){
        return memberExchangeService.messagingCount(memberId);
    }


    /**
     * 删除站内信，伪删除
     * @param memberId
     * @return
     */
    @ApiOperation(value = "删除站内信，伪删除")
    @CheckUserLogin
    @RequestMapping("/deleteMessaging")
    public RespBean deleteMessaging(@RequestParam Long memberId, @RequestParam Long messagingId){
        return memberExchangeService.deleteMessaging(memberId, messagingId);
    }

}
